﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using AgileZen.Models;

namespace AgileZen.Sample.Controllers
{
    public class StoryController : AgileZenController
    {
        public ActionResult Index(int projectid)
        {
            var stories = AgileZenService.GetStories(projectid);

            return View(stories);
        }
        
        public ActionResult Details(int id, int projectid)
        {
            var project = AgileZenService.GetStory(projectid, id, "details");

            return View(project);
        }

        public ActionResult Add(int projectid)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(int projectid, Story story)
        {
            try
            {
                AgileZenService.CreateStory(projectid, ref story);

                return RedirectToAction("Index", new { projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Edit(int projectid, int id)
        {
            var story = AgileZenService.GetStory(projectid, id );
            return View(story);
        }

        [HttpPost]
        public ActionResult Edit(int projectid, int id, Story story)
        {
            try
            {
                AgileZenService.UpdateStory(projectid,id, ref story);

                return RedirectToAction("Index", new { projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int projectid, int id)
        {
            try
            {
                AgileZenService.DeleteStory(projectid, id);

                return RedirectToAction("Index", new { projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Tags(int id, int projectid)
        {
            var project = AgileZenService.GetStory(projectid, id, Story.Enrichments.Tags);

            return View(project);
        }

        public ActionResult AddTag(int id, int projectid)
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddTag(int id, int projectid, Tag tag)
        {
            try
            {
                AgileZenService.CreateTag(projectid, id, ref tag);

                return RedirectToAction("Tags", new { id = id, projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult DeleteTag(int id, int projectid, int tagid)
        {
            try
            {
                AgileZenService.DeleteTag(projectid, id, tagid);

                return RedirectToAction("Tags", new { id = id, projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult SetTag(int projectid, int id, int tagid)
        {
            try
            {
                var stories = AgileZenService.GetStories(projectid).CurrentOrder;
                
                AgileZenService.SetStoriesForTag(projectid, tagid, stories.ToArray());

                return RedirectToAction("Tags", new { id = id, projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult SetTags(int projectid, int id, int tagid)
        {
            try
            {
                var story = AgileZenService.GetStory(projectid,id);
                var tags = (from s in AgileZenService.GetTags(projectid).Items select s.Id).ToArray();
                AgileZenService.SetTagsForStory(projectid, id, tags);

                return RedirectToAction("Tags", new { id = id, projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Tasks(int id, int projectid)
        {
            var project = AgileZenService.GetStory(projectid, id, "tasks");

            return View(project);
        }


        public ActionResult AddTask(int id, int projectid)
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddTask(int id, int projectid, Task task)
        {
            try
            {
                AgileZenService.CreateTask(projectid, id, ref task);

                return RedirectToAction("Tasks", new { id = id, projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Project/Edit/5

        public ActionResult EditTask(int storyid, int projectid, int id)
        {

            try
            {
                var task = AgileZenService.GetTask(projectid, storyid, id);
                return View(task);
            }
            catch
            {
                return View();
            }

        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        public ActionResult EditTask(int storyid, int projectid, int id, Task task)
        {
            try
            {
                // TODO: Add update logic here
                AgileZenService.UpdateTask(projectid, storyid, ref task);
                return RedirectToAction("Tasks", new { id = storyid, projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult DeleteTask(int id, int projectid, int taskid)
        {
            try
            {
                AgileZenService.DeleteTask(projectid, id, taskid);

                return RedirectToAction("Tasks", new { id = id, projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        
    }
}
